package com.itheima.dfs.controller;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.common.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ApiDfsController {


    @Autowired
    private FastFileStorageClient client;
    /**
     * 通过远程调用删除素材
     *
     * @param url
     * @return
     */
    @DeleteMapping("/api/wmMaterial/delete")
    public ResultVo delete(@RequestParam(value = "url") String url) {
        client.deleteFile(url);
        return ResultVo.ok();
    }



    /**
     * 通过远程调用dps下载图片素材(批量下载)
     *
     * @param
     * @return
     */
    @DeleteMapping("/api/dps/download")
    public ResultVo<List<byte[]>> download(@RequestBody List<String> images){
        List<byte[]> list =new ArrayList<>();
        if (CollectionUtils.isNotEmpty(images)){
            for (String imageUrl : images) {
                /**
                 * 下载整个文件
                 *
                 * @param groupName
                 * @param path
                 * @param callback
                 * @return
                 */
                StorePath storePath = StorePath.parseFromUrl(imageUrl);
                byte[] file = client.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
                list.add(file);
            }
        }
        return ResultVo.ok(list);
    }
}
